<template>
    <div class="main">
        <div class="content">
            <div class="breadcrumb">
                <div class="title">当前位置：</div>
                <el-breadcrumb separator="/">
                    <el-breadcrumb-item><a href="/admin">首页</a></el-breadcrumb-item>
                    <el-breadcrumb-item>商户</el-breadcrumb-item>
                    <el-breadcrumb-item>商户列表</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="cont">
                <div class="title">
                    <el-button type="primary" size="mini" @click="addShop">+添加多商户</el-button>
                    <div class="right">
                        <!-- <div class="item">
                            <el-select v-model="type" placeholder="请选择">
                                <el-option label="分类" :value="1"></el-option>
                                <el-option label="分类1" :value="2"></el-option>
                            </el-select>
                        </div>
                        <div class="item">
                            <el-select v-model="type" placeholder="请选择">
                                <el-option label="分类" :value="1"></el-option>
                                <el-option label="分类1" :value="2"></el-option>
                            </el-select>
                        </div> -->
                        <div class="item">
                            <el-select v-model="status" placeholder="状态" @change="changeStatus">
                                <el-option label="全部" value=""></el-option>
                                <el-option label="申请中" value="1"></el-option>
                                <el-option label="允许入驻" value="2"></el-option>
                                <el-option label="暂停中" value="3"></el-option>
                                <el-option label="申请失败" value="-1"></el-option>
                            </el-select>
                        </div>
                        <div class="item">
                            <el-input placeholder="搜索联系人姓名" v-model="nickname" class="input-with-select">
                                <el-button @click="search()" slot="append" icon="el-icon-search"></el-button>
                            </el-input>
                        </div>
                        <div class="item">
                            <el-input placeholder="搜索店铺名称" v-model="shopname" class="input-with-select">
                                <el-button @click="search()"  slot="append" icon="el-icon-search"></el-button>
                            </el-input>
                        </div>
                    </div>
                </div>
                <el-table
                    v-loading="loading"
                    :data="tableData"
                    style="width: 100%"
                    @selection-change="handleSelectionChange"
                >
                    <el-table-column type="selection" width="55"></el-table-column>
                    <el-table-column prop="id" label="ID"></el-table-column>
                    <el-table-column prop="shopname" label="商户名称"></el-table-column>
                    <el-table-column prop="mainprojects" label="主营项目"></el-table-column>
                    <el-table-column prop="nickname" label="联系人"></el-table-column>
                    <el-table-column prop="phone" label="联系电话"></el-table-column>
                    <el-table-column prop="status" label="入驻状态">
                        <template slot-scope="scope">
                            {{scope.row.status ==1?'待入驻':scope.row.status ==2?'允许入驻':'暂停中'}}
                        </template>
                    </el-table-column>
                    <el-table-column prop="createtime" label="创建时间"></el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="type">
                    <el-button size="mini" type="primary" @click="dialogVisible = true" icon="el-icon-circle-check">审核</el-button>
                    <!-- <el-button size="mini" type="primary" @click="changeType(3)" icon="el-icon-circle-close">禁用</el-button> -->
                    <el-button size="mini" type="primary" @click="delShop" icon="el-icon-delete">删除</el-button>
                </div>
                <div class="page">
                    <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-sizes="[10, 50, 100, 200]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
                    </el-pagination>
                </div>
            </div>
        </div>
        <!-- 弹窗 -->
        <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%"
        :close-on-click-modal="false"
        :before-close="handleClose">
            <div class="cont">
                <el-radio-group v-model="modifyType" @change="changeRadio">
                    <el-radio label="1">申请中</el-radio>
                    <el-radio label="2">允许入驻</el-radio>
                    <el-radio label="3">暂停中</el-radio>
                    <el-radio label="-1">申请失败</el-radio>
                </el-radio-group>
                <!-- <el-input
                v-show="modifyType==-1"
                type="textarea"
                :rows="2"
                placeholder="请输入拒绝原因"
                v-model="reason">
                </el-input> -->
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="handleClose">取 消</el-button>
                <el-button type="primary" @click="changeType">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data(){
        return{
            modifyType:'',
            reason:'',//拒绝原因
            dialogVisible: false,
            status:'',//状态
            shopname:'',//店铺名称
            nickname:'',//联系人名称
            loading:false,
            tableData:[],
            currentPage:1,//当前分页
            pageSize:10,//分页数量
            total:0,//数据总数
            type:'',
            ids:'',
        }
    },
    mounted(){
        this.getList()
    },
    computed:{
        
    },
    methods:{
        //修改状态
        changeRadio(){
            this.reason = ''
        },
        //关闭弹窗
        handleClose() {
            this.dialogVisible = false
            this.reason = ''
            this.modifyType = ''
        },
        //切换状态
        changeStatus(){
            this.currentPage = 1
            this.getList()
        },
        //搜索
        search(){
            
            this.currentPage = 1
            this.getList()
        },
        //添加商户
        addShop(){
            this.$router.push({ path: '/merchant/detail/none' })
        },
        //当前页面数据量改变
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
            this.pageSize = val
             this.currentPage = 1
            this.getList()
        },
        //翻页
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.currentPage = val
            this.getList()
        },
        //编辑
        handleEdit(index, row) {
            console.log(index, row);
            this.$router.push({ path:`/merchant/detail/${row.id}`})
        },
        //删除
        handleDelete(index, row) {
            console.log(index, row);
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.del(row.id)
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
        },
        delShop(){
            if (this.ids) {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.del(this.ids)
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });          
                });
            }else{
                this.$message('请选择商户');
            }
            
        },
        async del(e){
            let id = new FormData()
            id.append('ids',e)
            let{data:{status,result}} = await this.$http.post(this.$api.lineup_shop_destroy,id)
            if (status == 0) {
                this.$message(result.msg);
                this.getList()
            }
        },
        //当前选中项
        handleSelectionChange(val) {
            console.log(val);

            this.ids = val.map(item=>{
                return item.id
            }).join(',')

        },
        //获取列表
        async getList(){
            let req = new FormData()
            req.append('page',this.currentPage)
            req.append('pagesize',this.pageSize)
            req.append('shopname',this.shopname)
            req.append('nickname',this.nickname)
            req.append('status',this.status)
            this.loading = true
            let{data:{status,result}} = await this.$http.post(this.$api.shop_list,req)
            this.loading = false
            if (status == 0) {
                this.tableData = result.list
                this.total = Number(result.total)
            }
        },
        //禁用启用
        changeType(){
            if (this.ids) {
                // if (this.modifyType==-1) {
                //     if (this.reason) {
                //        this._changeType()
                //     }else{
                //         this.$message('请输入原因');
                //     }
                // }else{
                //     this._changeType()
                // }
                this._changeType()
                
            }else{
                this.$message('请选择商户');
            }
            
        },
        async _changeType(){
            this.$confirm('是否对当前商户状态进行更改?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.modifyStatus()
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });          
            });
            
        },
        async modifyStatus(){
            let ruleForm = new FormData()
            ruleForm.append('status',this.modifyType)
            ruleForm.append('reason',this.reason)
            ruleForm.append('ids',this.ids)
            let{data:{status,result}} = await this.$http.post(this.$api.lineup_shop_update_status,ruleForm)
            if (status == 0) {
                this.getList()
                this.dialogVisible = false
            }
        }
    }
}
</script>
<style lang="scss" scoped>
    .main{
        display: flex;
        .content{
            padding: 20px;
            width: 100%;
            .breadcrumb{
                display: flex;
                align-items: center;
            }
            .cont{
                padding: 20px 30px;
                background-color: #ffffff;
                margin-top: 15px;
                box-sizing: border-box;
                .title{
                    display: flex;
                    justify-content: space-between;
                    width: 100%;
                    .right{
                        display: flex;
                        justify-content: flex-end;
                        width: 800px;
                        .item{
                            margin: 0 5px;
                        }
                    }
                }
                .type{
                    padding: 20px 0;
                }
                .page{
                    text-align: center;
                    padding: 20px 0;
                }
            }
        }
    }
</style>